<div id="app">
    <admin-page-header back>
        <bi-button :loading="loading" @click="save" type="primary">保存</bi-button>
    </admin-page-header>

    <div class="container">
        <div class="row">
            <div class="col-12">
                <bi-card>
                    <bi-form ref="form">
                        <bi-form-item label="用户组名称">
                            <bi-input v-model="form.title" placeholder="请输入用户组名称" required />
                        </bi-form-item>
                        
                        <bi-form-item label="上级分组">
                            <admin-user-group v-model="form.parent_id" placeholder="请选择上级分组" power="manage" required root  :disabled-list="disabledList" />
                        </bi-form-item>
                
                        <bi-form-item label="权限">
                            <user-power v-model="form.power"  :group="form.parent_id" v-model:extend="form.power_extend" />
                        </bi-form-item>
                    </bi-form>
                </bi-card>
            </div>
        </div>
    </div>
</div>

<script>

import UserPower from 'field/user-power.vue';
 
let app = admin.createApp({
    components:{
        UserPower,
    },
    data(){

        let form ={
                group_id:'',
                parent_id:'',
                power:'',
                power_extend:{},
                title:'',
                update_at:''
            }

        let disabledList = [];
        if (this.$pageData.group){
            form = this.$assign(form,this.$pageData.group);
            if (this.$pageData.group.group_id) disabledList.push(this.$pageData.group.group_id)
        }

        
        
        return {
            form,
            loading:false,
            groupPower:'',
            disabledList
        }
    },
    methods:{
        save(){
            if (this.$refs.form.check()){

                if (this.$pageData.group && this.$pageData.group.group_id == this.form.parent_id){
                    this.$alert('上级分组不能是自己');
                    return false;
                }

                this.loading = true;
                this.$ajax({
                    url:this.$url({app:'admin','controller':'user','action':'groupSave'}),
                    data:this.form,
                    success:(data)=>{
                       
                       this.$user.delLocalStorage('admin-user-group-input');

                       this.$toast({text:'保存成功',type:'success'},()=>{
                            this.$go(-1);
                       });
                    },
                    over:()=>{
                    this.loading = false;
                    }
                });
            }


        },
    }
}).mount('#app');
</script>